package cn.netjava.robo3d.device;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Disk;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleMesh;

public class FireGun extends Node {

	public FireGun(DisplaySystem display, TriMesh geometry) {
		this.setName("FireGun");
		BlendState as1 = display.getRenderer().createBlendState();
		as1.setBlendEnabled(true);
		as1.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		as1.setDestinationFunction(BlendState.DestinationFunction.One);
		as1.setTestEnabled(true);
		as1.setTestFunction(BlendState.TestFunction.GreaterThan);
		as1.setEnabled(true);
		TextureState ts = display.getRenderer().createTextureState();
		ts.setTexture(TextureManager.loadTexture(CaseMate.class
				.getClassLoader().getResource(
						"cn/netjava/robo3d/images/star.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		ts.setEnabled(true);

		ParticleMesh mesh = ParticleFactory.buildParticles("particles", 10);
		mesh.setName("fire");
		mesh.setModelBound(new BoundingBox());
		mesh.setEmissionDirection(new Vector3f(0f, 0f, 1f));
		mesh.setMaximumAngle(0.0f);
		mesh.setSpeed(0.6f);
		mesh.setMinimumLifeTime(180.0f);
		mesh.setMaximumLifeTime(300.0f);
		mesh.setStartSize(0.1f);
		mesh.setEndSize(5.0f);
		mesh.setStartColor(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
		mesh.setEndColor(new ColorRGBA(0.6f, 0.2f, 0.0f, 0.0f));
		mesh.setInitialVelocity(0.1f);
		mesh.setRotateWithScene(true);
		Disk emitDisc = new Disk("disc", 6, 6, 0.5f);
		emitDisc.setLocalTranslation(new Vector3f(2.7f, 0f, 0));
		emitDisc.setCullHint(CullHint.Always);
		// mesh.setGeometry(geometry);
		mesh.forceRespawn();
		mesh.warmUp(60);

		mesh.setModelBound(new BoundingBox());
		// mesh.updateModelBound();
		ZBufferState zbuf = display.getRenderer().createZBufferState();
		zbuf.setWritable(false);
		zbuf.setEnabled(true);
		zbuf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
		mesh.setRenderState(ts);
		mesh.setRenderState(as1);
		mesh.setRenderState(zbuf);
		// Quaternion meshqua=new Quaternion();
		mesh.setLocalTranslation(new Vector3f(0f, 0.9f, 1f));
		this.attachChild(mesh);
		this.setModelBound(new BoundingBox());
		this.updateWorldBound();
	}

}
